package com.cwt_taxi_xa;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

import com.cwt_taxi_xa.model.LocalInfo;
import com.cwt_taxi_xa.util.DateUtil;
import com.cwt_taxi_xa.util.DecimalUtil;
import com.cwt_taxi_xa.util.StringUtil;
import com.cwt_taxi_xa.util.Tools;

import java.math.BigDecimal;

public class BusinessActivity extends CWTBaseActivity {

	private static final String TAG = "BusinessActivity";
	private TextView tv_load_time,tv_empty_time,tv_car_times,tv_wait_time,
			tv_mileage,tv_amount,tv_pay_type,tv_create_time,tv_mileage_empty;
	private LocalInfo info;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_buiness);
		setupViews();
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		info = (LocalInfo) getIntent().getSerializableExtra("info");
		if(info != null){
			String data = info.getData();
			String dataCreateTime = info.getTime();
			if(!StringUtil.isNull(dataCreateTime)){
				String createTime = DateUtil.getTime("20"+dataCreateTime, DateUtil.df6, DateUtil.df14);
				tv_create_time.setText(createTime);
			}else{
				tv_create_time.setText("时间错误");
			}
			byte[] dataByte = Tools.yxReversal(Tools.parseHexStr2Byte(data));
			dataByte = Tools.byteTobyte(dataByte, 13 + 25 + 25 + 15, dataByte.length-(13 + 25 + 25 + 15));

			int num = 0;
			num += 41;

			// 41	上车时间	BCD[5]	格式为YY-MM-DD-hh-mm
			String sTime = Tools.bcd2Str(Tools.byteTobyte(dataByte, num, 5));
			sTime = sTime + "00";
			sTime = DateUtil.getTime(sTime, DateUtil.df8, DateUtil.df12);
			num += 5;
			// 下车时间	BCD[2]	格式为hh-mm
			String eTime = Tools.bcd2Str(Tools.byteTobyte(dataByte, num, 2));
			if(eTime.length() == 3){
				eTime = "0"+eTime;
			}
			eTime = eTime + "00";
			eTime = DateUtil.getTime(eTime, DateUtil.df19, DateUtil.df12);
			num += 2;
			// 48	计程公里	BCD[3]	格式为XXXXX.Xkm
			BigDecimal decimaMil = new BigDecimal(Tools.bcd2Str(Tools.byteTobyte(dataByte, num, 3)));
			decimaMil = decimaMil.divide(new BigDecimal(10));
			String milege = DecimalUtil.priceFormat(decimaMil.doubleValue());
			num += 3;
			// 51	空驶里程	BCD[2]	格式为XXX.Xkm
			BigDecimal emptyMil = new BigDecimal(Tools.bcd2Str(Tools.byteTobyte(dataByte, num, 2)));
			emptyMil = emptyMil.divide(new BigDecimal(10));
			String emptyMilege = DecimalUtil.priceFormat(emptyMil.doubleValue());
			num += 2;
			// 附加费	BCD[3]	格式XXXXX.X元
			num += 3;
			// 等待计时时间	BCD[2]	格式为hh-mm
			String waitTime = Tools.bcd2Str(Tools.byteTobyte(dataByte, num, 2));
			if(waitTime.length() == 3){
				waitTime = "0"+waitTime;
			}
			waitTime = waitTime + "00";
			waitTime = DateUtil.getTime(waitTime, DateUtil.df19, DateUtil.df12);
			num += 2;
			// 70	交易金额	BCD[3]	格式XXXXX.X元（总金额）
			BigDecimal decimal = new BigDecimal(Tools.bcd2Str(Tools.byteTobyte(dataByte, num, 3)));
			decimal = decimal.divide(new BigDecimal(10));
			String amount = DecimalUtil.priceFormat(decimal.doubleValue());
			num += 3;
			// 74	当前车次	UINT32
			int carCount = Integer.parseInt(Tools.parseByte2HexStr(Tools.byteTobyte(dataByte, num, 4)), 16);
			num += 4;
			//	78 	账单类型	BYTE
			int payType = Integer.parseInt(Tools.parseByte2HexStr(Tools.byteTobyte(dataByte, num, 1)), 16);
			System.out.println("payType----------------:"+payType);

			tv_empty_time.setText(eTime);
			tv_load_time.setText(sTime);
			tv_car_times.setText(""+carCount);
			tv_amount.setText(amount.substring(0, amount.indexOf("."))+"元");
			tv_amount.setTextColor(Color.parseColor("#FEE801"));
			/*if(info.getStatus() == 0){
				//未上传
				tv_amount.setTextColor(Color.parseColor("#FF0000"));
			}else{
				tv_amount.setTextColor(Color.parseColor("#FEE801"));
			}*/
			if(payType == 0){
				tv_pay_type.setText("现金");
			}else if(payType == 1){
				tv_pay_type.setText("M1卡");
			}else if(payType == 3){
				tv_pay_type.setText("CPU卡");
			}else if(payType == 9){
				tv_pay_type.setText("其他");
			}
			tv_mileage.setText(milege);
			tv_wait_time.setText(waitTime);
			tv_mileage_empty.setText(emptyMilege);
		}
	}

	private void setupViews() {
		// TODO Auto-generated method stub
		tv_load_time = (TextView)this.findViewById(R.id.tv_load_time);
		tv_empty_time = (TextView)this.findViewById(R.id.tv_empty_time);
		tv_wait_time = (TextView)this.findViewById(R.id.tv_wait_time);
		tv_mileage = (TextView)this.findViewById(R.id.tv_mileage);
		tv_car_times = (TextView)this.findViewById(R.id.tv_car_times);
		tv_amount = (TextView)this.findViewById(R.id.tv_amount);
		tv_pay_type = (TextView)this.findViewById(R.id.tv_pay_type);
		tv_create_time = (TextView)this.findViewById(R.id.tv_create_time);
		tv_mileage_empty = (TextView)this.findViewById(R.id.tv_mileage_empty);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		info = null;
	}

	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub

	}
}
